<template>
  <div ref="main" :style="{height: height, width: width}" :class="className">

  </div>
</template>

<script>
import * as echarts from 'echarts';
// 引入主题
require('echarts/theme/macarons')
export default {
    props:{
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '400px'
        },
        legendData: {
            type: Array,
            default: () => ['前端', 'Java', '移动端', '大数据']
        },
        seriesData: {
            type: Array,
            default: () => [
                {value: 100, name: '前端'},
                {value: 200, name: 'Java'},
                {value: 300, name: '移动端'},
                {value: 500, name: '大数据'},
            ]
        }
    },
    data () {
        return {
            chart: null,     // echart实例
        };
    },

    // 组件销毁之前调用这个钩子函数
    beforeDestroy(){
        if(!this.chart){
            return;
        }
        this.chart.dispose();
        this.chart = null;
    },

    mounted(){
        this.$nextTick(() => {
            this.initChart();
        })
    },

    methods: {
        initChart(){
            // this.chart = echarts.init(this.$refs.main, 'macarons');
            this.chart = echarts.init(this.$refs.main);
            this.chart.setOption({
                title: {
                    text: '技术频道文章统计',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b} : {c} ({d}%)'
                },
                legend:{
                    orient: 'vertical',
                    left: 'left',
                    data: this.legendData
                },
                series: [
                    {
                        name: '统计内容',
                        type: 'pie',
                        radius: '55%',
                        center: ['45%','60%'],
                        data: this.seriesData,
                        empjasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.9)'
                            }
                        }
                    }
                ]
            })
        },
    }
}

</script>
<style lang='scss' scoped>
</style>
